package com.education.wisdom.controller.app;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class zfbPayController {

    private String APP_ID = "2016102500760434" ;
    private String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCVfivImyQORANCGMdGvoMYvthsDTmUxOWrnQmJg006Ezhzyqq7vibEy9CTZYsYoRg0IkPWIIWlYP4d62hJm/5jekemuFkpFThTmiLNxTxilz3IF4J9zTH9mvIGG7w2LYEqSuLYLw2g9Y8ncgVP2Dh15Do/4mTMidYkn3Fs/gOeSxzuQzJlfplAJzGP8FUzhB/49HyV5UdP8KpISoYCeJyY2iQqm0wJLUAb9j5ltvx2S9dz7Lm2EslNX+zb+sxaUONhapcnZAZH/+lBsrG5+mxdnQetx+KgIOtgeX+QcROQgiKQHVIRM/lX2JzokXpOr1ji3pxpC4ULl7MwOFGJYFe/AgMBAAECggEBAI0zhYuP7jiDSUiPOvq5S/59lNJKk47Pg9fbFn0KxlEKgpHsZeO7McdEdBglG4MB9miXzNdeFJ4mxR/hkLmmXFv9zdvN2HmTj4Z32lPd7rF5BWQPwLOus39FcUEPxXCu/ofCAA4OM6NLDhJGAnpuUrYYAmUBJBCGSPyClZJLQ4OpHzRnW+EvG4cmlqxqVz14VNCzVf+Dk1uOUN96AxlFgY9E9B6l7JVhtFAqUkqGzVuyrQQ4xxmvXCoJwrrJK/vMAcTiGeB6USEE9zJDwyr0mIfDV+6PyYqO/NrPytLGGTwO9bzI+mUC+eHfBM/dDHLB3yC7RDf3bXJ0vPwhtDj3IxECgYEA2nQEG6qy/tA+HWYxzLwQ89aThFLvXJfRruvOPjz+zru7r0sHurJw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SMweuyHfnA/PvmNPGrQMoC1pzG8C3UzfJogrsWZrBIe3jxx7VzDMYZ8miwu47FtvhFC3AcdJvTcZb513USHcu5z4m8A3ezhpboXW+lsXs26prL83XsUptWgYmRz413VyyjqaITpbW85Pc/85dv6IfQIDAQAB" ;
    private String CHARSET = "UTF-8" ;

    @GetMapping("/test")
    public void doPost(HttpServletRequest httpRequest,
                       HttpServletResponse httpResponse) throws ServletException, IOException {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101002\"," +
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

}
